package com.aqie.medium.dfs;

import com.aqie.easy.structure.TreeNode;

/**
 * 114 二叉树展开为链表
 * todo
 *
 */
public class Flatten {
    /**
     * 1. 根 -> 左 -> 右 ：前序遍历
     * 2.
     * 3.
     * @param root
     */
    TreeNode last = null;
    public void flatten(TreeNode root) {
        if (root == null) return;
        if (last != null){
            last.left = null;
            last.right = root;
        }
        last = root;
        // 备份右子节点
        TreeNode copyRight = root.right;
        flatten(root.left);
        flatten(copyRight);
    }
}
